require 'login'
require 'miscfunc'
require 'hpricot'
require 'open-uri'

class Kaixin < Login
    include MiscFunc

    def goto_house
        info "redirect to house game"
        @house_url = 'http://www.kaixin001.com/!house/index.php'
        @ie_for_kaixin.goto @house_url
    end

    def goto_joblist
        info "redirect to joblist game"
        @joblist_url = 'http://www.kaixin001.com/!house/jobslist.php'
        @ie_for_kaixin.goto @joblist_url
    end

    def get_house_config
      @ie_for_kaixin.goto @house_url
      File.open(".tmp/myhouse.html", 'w+b') { |file| file.puts(@ie_for_kaixin.html) }
      info "File myhouse.html saved"
        house_doc = open(".tmp/myhouse.html") { |mtf| Hpricot(mtf)}
        house_doc.search("script").each do |h|
          v = h.to_s.scan(/g_verify = "(.*)"/).to_s
          if (v != "")
            @verify = v
            @roomid = h.to_s.scan(/roomid = "(.*)"/).to_s
          end
        end
        
    end

    def job
        begin
            do_job
        rescue => err
            error err
            error "Your password might be wrong!!!"
            raise "PasswordWrong"
        end
    end
    
    def do_job
      @ie_for_kaixin.goto @joblist_url
      File.open(".tmp/joblist.html", 'w+b') { |file| file.puts(@ie_for_kaixin.html) }
      info "File joblist.html saved"
      
      joblist_doc = open(".tmp/joblist.html") { |jlf| Hpricot(jlf)}
      miss = joblist_doc.search("img[src='http://img.kaixin001.com.cn/i2/house/lady.gif']").to_s

      if (miss != "")
        working = true
      else
        working = false            
      end
      if !working
        job_value = joblist_doc.search("input[@name='petid']").last.attributes["value"]
        debug "#{job_value}"
        @ie_for_kaixin.radio(:value, "#{job_value}").set
        @ie_for_kaixin.button(:name, "input").click
        sleep 2
        frame = @ie_for_kaixin.frame(:id, "iframe_parent")
        frame.button(:index=>1).click
        info "You are doing job #{job_value}"
        sleep 5
      else
        info "You are already working"
      end
    end   
    
    def find_homeless_friends
      info "redirect to house game"
      homeless_friends_url = "http://www.kaixin001.com/house/friendfree_dialog.php?verify=#{@verify}"
      info "#{homeless_friends_url}"
      @ie_for_kaixin.goto homeless_friends_url
      File.open(".tmp/myhomeless.html", 'w+b') { |file| file.puts(@ie_for_kaixin.html) }
      info "File myhomeless.html saved"
      sleep 3
      homeless_friends = []
      homeless_doc = open(".tmp/myhomeless.html") { |mtf| Hpricot(mtf)}
      homeless_doc.search("a[@class = 'sl']").each do |h|
        hf = h.to_s.scan(/\((.*)\)/)
        name = h.inner_html
        message "#{name}  (#{hf})"
        homeless_friends = homeless_friends << hf
      end
      return homeless_friends
    end
    
    def stay_friends(hf)
      
      s = rand(hf.length)
      user = hf[s]
      info "will stay #{user}"
      user_url = "http://www.kaixin001.com/!house/index.php?fuid=#{user}"
      @ie_for_kaixin.goto user_url
      #document.parentWindow.execScript("gotoUser(#{user})")
    end
    
    def stay_me()
      @ie_for_kaixin.goto @house_url
    end
    

def get_garden
  garden_doc = open("htmls/mygarden.html") { |f| Hpricot::XML(f)}
  garden_doc.search("garden").search("item").each do |h|
    fuid = h.search("fuid").text
    cropsid = h.search("cropsid").text
    puts "fuid: #{fuid}"
    puts "cropsid: #{cropsid}"
    if cropsid != "0"
      name = h.search("name").inner_html
      crops = h.search("crops").inner_html.split("&lt;br /&gt;")
      puts "crops.length #{crops.length}"
      if crops.length == 3
        time_info = crops[2].scan(/(\d+)/)
        tl = time_info.length
        if tl <= 3
          
          time_left = ""
          time_info.each do |t|
            time = case
                 when tl == 3: "day"
                 when tl == 2: "hour"
                 when tl == 1: "min"
                 end
            tl = tl-1
            time_left = time_left+t.to_s+time
            #puts "#{time_left}"
          end
        end
      end
      puts "name: #{name}"
      puts "time_left: #{time_left}"    
    end
  end  
end

def get_farm
  farm_doc = open("htmls/myfarm.html") { |f| Hpricot::XML(f)}
  farm_doc.search("product2").search("item").each do |h|
    name = h.search("pname").text
    if name != ""
      num = h.search("num").text
      stealnum = h.search("stealnum").text
      puts "name: #{name}"
      puts "num: #{num} stealnum: #{stealnum}"    
      puts "so, you have #{num.to_i - stealnum.to_i} now"
    end
  end
  water = farm_doc.search("water").text
  grass = farm_doc.search("grass").text
  puts "water: #{water}"
  puts "grass: #{grass}"
  farm_doc.search("animals").search("item").each do |h|
    animalsid = h.search("animalsid").text
    aname = h.search("aname").text
    puts "animalsid: #{animalsid}"
    puts "aname: #{aname}"
  end
end

def get_fish
  fish_doc = open("htmls/myfish.html") { |f| Hpricot::XML(f)}
  fish_doc.search("fish").search("item").each do |h|
    name = h.search("skey").text
    if name != ""
      tips = h.search("tips").text
      puts "name: #{name}"
      puts "tips: #{tips}"    
    end
  end 
  fish_doc.search("fisher").search("item").each do |h|
    uid = h.search('uid').text
    pos = h.search('pos').text
    tackleid = h.search('tackleid').text
    real_name = h.search('real_name').text
    puts "uid: #{uid}"
    puts "pos: #{pos}"
    puts "real name: #{real_name}"
  end
end
    
    def show_friends_garden
      garden_url = "http://www.kaixin001.com/!house/garden/index.php"
      @ie_for_kaixin.goto garden_url
      3.times {
        @ie_for_kaixin.document.parentWindow.execScript("gotoRFriend()") 
        sleep 10
      }
      
    end

end
